// pages/register/registerAndAppiointHistory.js
const cache = require("../../utils/cache")
const api = require("../../utils/api")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    TabCur: 0,
    currentPatient: '', // 当前就诊人
    registerRecordList: [],//挂号记录查询
    appointRecordList:[],//预约记录查询
  },
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
    })
    this.loadList()
  },
  showModal(event){
    this.setData({
      modalName: event.currentTarget.dataset.target
    })
  },
  callbackLoad(e) {
    this.loadPatient();
    this.loadList();
  },
  loadPatient() {
    let cachePt = cache.currentPatient();
    if (cachePt != undefined && cachePt != null) {
      this.setData({
        currentPatient: cachePt
      })
    }
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  handleRegisterDetail(event){
    let cur = {}
    let tbAppointId = event.currentTarget.dataset.id
    if(this.data.TabCur==0){
     cur = this.data.registerRecordList.find(m=>m.tbAppointId == tbAppointId)
    }else{
      cur = this.data.appointRecordList.find(m=>m.tbAppointId == tbAppointId)
    }
    if(cur==undefined || cur==null || cur==''){
      return
    }
    
    wx.navigateTo({
      url: '/pages/register/registerHistoryDetail?oper='+(this.data.TabCur==0?'register':'appoint'),
      success: function(res){
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('record', { data: cur })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id != undefined) {
      this.setData({
        TabCur: options.id
      })
    }
   
  },
loadList(){
  let ioNo = this.data.currentPatient.ioNo
  if(ioNo==undefined || ioNo== null || ioNo==''){
    return;
  }
  let that = this
  if(this.data.TabCur==0){
    api.getRegisterRecord({
      ioNo
    }).then(res=>{
      that.setData({
        registerRecordList: res
      })
    })
  }else{
    api.getAppointRecord({
      ioNo
    }).then(res=>{
      that.setData({
        appointRecordList: res
      })
    })
  }
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadPatient()
    this.loadList()
    let that = this.selectComponent("#card")
    that.callbackFunc(() => that.loadData())
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})